package siruna.WE;

import java.io.*;

import android.content.*;
import android.database.*;
import android.net.*;
import android.os.*;

/**
 * Probleem met het aanspreken en bekijken van lokale files in de webview.
 * Workaround via deze klasse. Meer info:
 * http://www.techjini.com/blog/2009/01/10
 * /android-tip-1-contentprovider-accessing
 * -local-file-system-from-webview-showing-image-in-webview-using-content/
 * 
 *@author Tim
 */
public class WebLocalFileContentProvider extends ContentProvider {
	private static final String URI_PREFIX = "content://com.tourizo.android.localfile";

	/*
	 * Hulpmethode om het pad te construeren
	 */
	public static String constructUri(String url) {
		Uri uri = Uri.parse(url);
		return uri.isAbsolute() ? url : URI_PREFIX + url;
	}

	/*
	 * Methode die ervoor zorgt dat bestanden met een bepaald pad hier worden
	 * onderschept en manueel het juiste bestand wordt teruggegeven
	 * 
	 * @return ParcelFileDescriptor
	 */
	@Override
	public ParcelFileDescriptor openFile(Uri uri, String mode)
			throws FileNotFoundException {
		System.out.println("openFile (WebLocalFileContentProvider): " + uri);
		String path = "";
		if (uri.toString().contains("/temp/")) {
			path = "data/data/siruna.WE/files/" + uri.getPath();

		} else {
			path = "data/data/siruna.WE/files/widgets" + uri.getPath();
		}
		System.out.println("abspath:  " + path);

		File file = new File(path);
		ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file,
				ParcelFileDescriptor.MODE_READ_ONLY);
		return parcel;
	}

	@Override
	public boolean onCreate() {
		return true;
	}

	@Override
	public int delete(Uri uri, String s, String[] as) {
		throw new UnsupportedOperationException(
				"Not supported by this provider");
	}

	@Override
	public String getType(Uri uri) {
		throw new UnsupportedOperationException(
				"Not supported by this provider");
	}

	@Override
	public Uri insert(Uri uri, ContentValues contentvalues) {
		throw new UnsupportedOperationException(
				"Not supported by this provider");
	}

	@Override
	public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
		throw new UnsupportedOperationException(
				"Not supported by this provider");
	}

	@Override
	public int update(Uri uri, ContentValues contentvalues, String s,
			String[] as) {
		throw new UnsupportedOperationException(
				"Not supported by this provider");
	}

}